﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;

namespace f1
{
   
    public partial class Form1 : Form
    {
        //public static Order[] ilist=new Order[100];
        //public int len_list=0;
        public Form1()
        {
            InitializeComponent();
            OrderService os = new OrderService();
            ColumnHeader c1 = new ColumnHeader();
            ColumnHeader c2 = new ColumnHeader();
            ColumnHeader c3 = new ColumnHeader();
            ColumnHeader c4 = new ColumnHeader();
            c1.Width = 80;
            c2.Width = 100;
            c3.Width = 100;
            c4.Width = 100;
            c1.Text = "订单号";
            c2.Text = "产品";
            c3.Text = "顾客";
            c4.Text = "金额";
            lv.GridLines = true;  //显示网格线
            lv.FullRowSelect = true;  //显示全行
            lv.MultiSelect = false;  //设置只能单选
            lv.View = View.Details;  //设置显示模式为详细
            lv.ShowGroups = false;
            //lv.HoverSelection = true;  //当鼠标停留数秒后自动选择
            lv.Columns.Add(c1);
            lv.Columns.Add(c2);
            lv.Columns.Add(c3);
            lv.Columns.Add(c4);
            OrderService.importList();
            OrderService.selectAll();



        }
        //~Form1()
        //{
       //     OrderService.exportList();
        //}

        private void btn_add_Click(object sender, EventArgs e)
        {
            Form_add fa = new Form_add();
            fa.Show();
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
            Form_select fs = new Form_select();
            fs.Show();
           
        }

        private void button3_Click(object sender, EventArgs e)
        {//删除
            Form_delete fd = new Form_delete();
            fd.Show();
        }

        private void btn_update_Click(object sender, EventArgs e)
        {
            Form_update fu = new Form_update();
            fu.Show();

        }
    }
    public class Order
    {
        public int orderNum;
        public string product;

        public string customer;

        public double price;
        public void setOrderNum(int on)
        {
            this.orderNum = on;
        }
        public void setProduct(string p)
        {
            this.product = p;
        }
        public void setCustomer(string c)
        {
            this.customer = c;
        }
        public void setPrice(double pri)
        {
            this.price = pri;
        }
        public int getOrderNum()
        {
            return this.orderNum;
        }
        public string getProduct()
        {
            return this.product;
        }
        public string getCustomer()
        {
            return this.customer;
        }
        public double getPrice()
        {
            return this.price;
        }
        public Order(int num,String pro,String cus,double pri)
        {
            this.orderNum = num;
            this.product = pro;
            this.customer = cus;
            this.price = pri;
        }
        public Order() { }
    }
    public class OrderService
    {
        public static Order[] orderList = new Order[100];
        static int len = 0;
        public static void XmlSerialize(XmlSerializer ser, string filename, object obj)
        {
            FileStream fs = new FileStream(filename, FileMode.Create);
            ser.Serialize(fs, obj);
            fs.Close();

        }
        public static object XmlDeserialize(XmlSerializer ser, string filename)
        {
            try
            {
                FileStream fs = new FileStream(filename, FileMode.Open);
                object obj = ser.Deserialize(fs);
                fs.Close();
                return obj;
            }
            catch
            {
                return null;
            }


        }
        public static void exportList()//将所有的订单序列化为XML文件
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(Order[]));
            String xmlFileName = "order1.xml";
            XmlSerialize(xmlser, xmlFileName, orderList);
            string xml = File.ReadAllText(xmlFileName);
            Console.WriteLine(xml);
        }
        public static void importList()
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(Order[]));
            orderList = XmlDeserialize(xmlser, "order1.xml") as Order[];
            if (orderList != null)
            {
                 for (int i = 0; orderList[i] != null; i++)
                 {
                    len++;
                 }
            }
           
        }
        public static bool addOrder(int num,String pro,String cus,double pri)
        {
            try
            {
                
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(len);
                bool okToAdd = true;
                foreach (var item in allOrders)
                {                   
                    if (item != null)
                    {
                        if (item.getOrderNum() == num | (item.getProduct() == pro && item.getCustomer() == cus && item.getPrice() == pri))
                        {//保证每个订单的明细不重复
                            okToAdd = false;
                        }
                    }


                }
                if (okToAdd)
                {
                    Order o = new Order(num, pro, cus, pri);
                    //Console.WriteLine(orderList.Length);
                    orderList[len] = o;
                    len++;
                    OrderService.exportList();
                    //DialogResult result = MessageBox.Show("添加成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    return true;

                    //Console.WriteLine("添加成功");
                    

                }
                else
                {
                    //Console.WriteLine("添加失败，已有相同订单")
                    //DialogResult result = MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return false;
                }
            }
            catch
            {
                //DialogResult result = MessageBox.Show("添加失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;  
            }
        }
        public static bool deleteOrder(int num)
        {
            try {
                //bool find = false;
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(orderList.Length);
                int n = 0;
                foreach (var item in allOrders)
                {
                    n++;
                    if (item.getOrderNum() == num)
                    {
                        //find = true;
                        //int j = n;
                        while (n < orderList.Length)
                        {
                            orderList[n - 1] = orderList[n];
                            n++;
                        }

                        //Console.WriteLine("删除成功");
                        len--;
                        OrderService.exportList();
                        return true;
                    }
                }

                //Console.WriteLine("无此订单");
               // DialogResult result = MessageBox.Show("删除失败", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
                
            }
            catch
            {
                return false;
            }
            
        }
        public static bool updateOrder(int num, String pro, String cus, double pri)
        {
            //bool find = false;
            var allOrders = from item in orderList where item != null select item;
            allOrders = allOrders.Take(len);
            int n = 0;
            foreach (var item in allOrders)
            {
                if (item.getOrderNum() == num)
                {
                    //find = true;
                    bool okToUpdate = true;
                    var allOrders2 = from item2 in orderList where item2 != null select item2;
                    allOrders2 = allOrders2.Take(len);

                    foreach (var item2 in allOrders2)
                    {
                        if (item.getProduct() == pro && item.getCustomer() == cus && item.getPrice() == pri)
                        {
                            okToUpdate = false;
                        }
                    }
                    if (okToUpdate)
                    {
                        orderList[n].setProduct(pro);
                        orderList[n].setCustomer(cus);
                        orderList[n].setPrice(pri);
                        //find = true;
                        //Console.WriteLine("修改成功！");
                        OrderService.exportList();
                        return true;
                    }
                    else
                    {
                        //Console.WriteLine("修改失败，已有相同订单");
                        return false;
                    }
                }
                n++;
            }
            return false;

        }

        public static void selectAll()
        {
            Form1.lv.Items.Clear();
            if (orderList != null)
            {
                var allOrders = from item in orderList where item != null select item;
                allOrders = allOrders.Take(len);
                //int l = 0;
                //Order[] list = new Order[100];
                foreach(var item in allOrders)
                {
                //插入列表
                    ListViewItem a = new ListViewItem();
                    a.Text = Convert.ToString(item.getOrderNum());
                    a.SubItems.Add(item.getProduct());
                    a.SubItems.Add(item.getCustomer());
                    a.SubItems.Add(Convert.ToString(item.getPrice()));
                    Form1.lv.Items.Add(a);
                
                }
            }
            
            
            
                            
        }

        public static void selectNum(int num)
        {
            Form1.lv.Items.Clear();
            var allOrders = from item in orderList where item != null && item.getOrderNum()==num select item ;
            foreach (var item in allOrders)
            {
                //插入列表
                ListViewItem a = new ListViewItem();
                a.Text = Convert.ToString(item.getOrderNum());
                a.SubItems.Add(item.getProduct());
                a.SubItems.Add(item.getCustomer());
                a.SubItems.Add(Convert.ToString(item.getPrice()));
                Form1.lv.Items.Add(a);
            }
        }
        public static void selectCus(string cus)
        {
            Form1.lv.Items.Clear();
            var allOrders = from item in orderList where item != null && item.getCustomer() == cus select item;
            foreach (var item in allOrders)
            {
                //插入列表
                ListViewItem a = new ListViewItem();
                a.Text = Convert.ToString(item.getOrderNum());
                a.SubItems.Add(item.getProduct());
                a.SubItems.Add(item.getCustomer());
                a.SubItems.Add(Convert.ToString(item.getPrice()));
                Form1.lv.Items.Add(a);
            }
        }
        public static void selectPro(string pro)
        {
            Form1.lv.Items.Clear();
            var allOrders = from item in orderList where item != null && item.getProduct() == pro select item;
            foreach (var item in allOrders)
            {
                //插入列表
                ListViewItem a = new ListViewItem();
                a.Text = Convert.ToString(item.getOrderNum());
                a.SubItems.Add(item.getProduct());
                a.SubItems.Add(item.getCustomer());
                a.SubItems.Add(Convert.ToString(item.getPrice()));
                Form1.lv.Items.Add(a);
            }
        }

        public static void selectPri(double pri)
        {
            Form1.lv.Items.Clear();
            var allOrders = from item in orderList where item != null && item.getPrice() == pri select item;
            foreach (var item in allOrders)
            {
                //插入列表
                ListViewItem a = new ListViewItem();
                a.Text = Convert.ToString(item.getOrderNum());
                a.SubItems.Add(item.getProduct());
                a.SubItems.Add(item.getCustomer());
                a.SubItems.Add(Convert.ToString(item.getPrice()));
                Form1.lv.Items.Add(a);
            }
        }

    }
}
